package swing;

import javax.swing.*;
import java.awt.*;

/**
 * Created with IntelliJ IDEA.
 * User: nuno-rafael-figueiredo
 * Date: 20-02-2014
 * Time: 18:03
 */
public class GridPanel extends JPanel {
    /**
	 * 
	 */
	private static final long serialVersionUID = 5836657025797110048L;
	private final GridBagConstraints constraints;
    private int lastY = -1;

    public GridPanel() {
        super(new GridBagLayout());
        constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.HORIZONTAL;
    }

    protected JLabel set(int x, int y, String text) {
        return set(x, y, new JLabel(text));
    }

    public <T extends Component> T set(int x, int y, T component) {
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridx = x;
        constraints.gridy = lastY = y;
        add(component, constraints);

        return component;
    }

    public JLabel addLabel(int y, String label, String message){
        set(0, y, new JLabel(label));

        return set(1, y, new JLabel(message));
    }

    protected JLabel addLabel(String label, Object message) {
        return addLabel(++lastY, label, message.toString());
    }
}
